package com.huatech.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huatech.common.export.ExportModel;
import com.huatech.common.export.ExportUtil;
import com.huatech.common.util.BeanMapUtils;
import com.huatech.domain.SystemConfig;
import com.huatech.model.SystemConfigModel;
import com.huatech.service.SystemConfigService;

/**
 * 系统配置管理
 * 
 * @author lh
 * @version 2.0
 */
@Controller
public class SystemConfigController extends BaseController {

	Map<String, Object> data = new HashMap<>();

	private static final Logger logger = LoggerFactory.getLogger(SystemConfigController.class);

	@Resource
	private SystemConfigService systemConfigService;

	@RequestMapping(value = "/modules/system/config/manage", method = RequestMethod.GET)
	public String manage() {
		return "/modules/system/config/manage";
	}

	/**
	 * 查询系统配置信息
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/modules/system/config/manageList", method = RequestMethod.POST)
	@ResponseBody
	public String manageList(SystemConfigModel model) {
		Map<String, Object> data = new HashMap<>();
		Map<String, Object> paramMap = new HashMap<>();
		PageHelper.startPage(model.getPage(), model.getRows());
		Page<SystemConfig> page = (Page<SystemConfig>) systemConfigService.findList(paramMap);

		data.put("total", page.getTotal()); // 总行数
		data.put("rows", page.getResult()); // 集合对象
		return JSON.toJSONString(data);
	}

	/**
	 * 系统配置页面输入页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/modules/system/config/append", method = RequestMethod.POST)
	public String append() {
		return "/modules/system/config/append";
	}

	/**
	 * 添加系统配置
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/modules/system/config/doAppend", method = RequestMethod.POST)
	@ResponseBody
	public String doAppend(SystemConfigModel model) throws Exception {
		Map<String, Object> data = new HashMap<>();
		try {
			if (null == model) {
				throw new RuntimeException("model can not be null!");
			}
			// 校验参数
			model.validParam();

			// 填写补充信息
			model.setCreateTime(new Date());
			model.setStatus(1);

			systemConfigService.save(model.copyToDomain());
			data.put("result", true);
			data.put("msg", "success");
		} catch (Exception e) {
			logger.error("addMer_error", e);
			data.put("result", false);
			data.put("msg", e.getMessage());
		}
		return JSON.toJSONString(data);
	}

	/**
	 * 查看系统配置信息
	 */
	@RequestMapping(value = "/modules/system/config/view", method = RequestMethod.POST)
	public String view(SystemConfigModel model) throws Exception {
		getRequest().setAttribute("model", systemConfigService.find(model.getId()));
		return "/modules/system/config/view";
	}

	/**
	 * 修改系统配置信息
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/modules/system/config/modify", method = RequestMethod.POST)
	public String modify(SystemConfig model) throws Exception {
		getRequest().setAttribute("model", systemConfigService.find(model.getId()));
		return "/modules/system/config/modify";
	}

	/**
	 * 修改系统配置信息
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/modules/system/config/doModify", method = RequestMethod.POST)
	@ResponseBody
	public String updateSystemConfig(SystemConfig model) {
		Map<String, Object> data = new HashMap<>();
		data.put("result", true);
		data.put("msg", "success");
		try {
			systemConfigService.updateSelective(model);
		} catch (Exception e) {
			logger.error("update_mer_error", e);
			data.put("result", false);
			data.put("msg", e.getMessage());
		}
		return JSON.toJSONString(data);
	}
	
	/**
	 * 系统配置Excel导出
	 * @param model
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/modules/system/config/export" , method = RequestMethod.GET)
	public void exportSystemConfig(SystemConfigModel model, HttpServletRequest request, HttpServletResponse response)throws Exception {
		
		ExportUtil.exportExcel(new ExportModel() {

			private Map<String, Object> bean2Map(){
				return BeanMapUtils.bean2Map(model);
			}
			
			@Override
			public int getCount() {
				return systemConfigService.getCount(bean2Map());
			}

			@Override
			public List<?> getPageRecords() {
				return systemConfigService.findList(bean2Map());
			}
			
		}, request, response);
	}
	

}
